<template>
  <div class="addQuestionView">
    <h2>Create Question</h2>
    <a-form :model="form" label-align="left">
      <a-form-item field="title" label="Title">
        <a-input v-model="form.title" placeholder="Enter the title" />
      </a-form-item>
      <a-form-item field="tags" label="Tags">
        <a-input-tag v-model="form.tags" placeholder="Enter tags" allow-clear />
      </a-form-item>
      <a-form-item field="content" label="Problem Description">
        <MdEditor :value="form.content" :handle-change="onContentChange" />
      </a-form-item>
      <a-form-item field="answer" label="Answer">
        <MdEditor :value="form.answer" :handle-change="onAnswerChange" />
      </a-form-item>
      <a-form-item label="Conifg" :content-flex="false" :merge-props="false">
        <a-space direction="vertical" style="min-width: 480px">
          <div id="group">
            <a-button
              type="primary"
              style="min-width: 60px"
              @click="changeConfig(1)"
              >Type 1
            </a-button>
            <a-button
              type="primary"
              style="min-width: 60px"
              @click="changeConfig(2)"
              >Type 2
            </a-button>
            <a-button
              type="primary"
              style="min-width: 60px"
              @click="changeConfig(3)"
              >Type 3
            </a-button>
          </div>
          <a-form-item field="judgeConfig.timeLimit" label="TimeLimit(ms)">
            <a-input-number
              v-model="form.judgeConfig.timeLimit"
              placeholder="TimeLimit"
              mode="button"
              min="1000"
              size="large"
            />
          </a-form-item>
          <a-form-item field="judgeConfig.memoryLimit" label="MemoryLimit">
            <a-input-number
              v-model="form.judgeConfig.memoryLimit"
              placeholder="MemoryLimit"
              mode="button"
              min="10000"
              size="large"
            />
          </a-form-item>
          <!-- <a-form-item field="judgeConfig.stackLimit" label="CPULimit">
            <a-input-number
              v-model="form.judgeConfig.stackLimit"
              placeholder="CPULimit"
              mode="button"
              min="0"
              size="large"
            />
          </a-form-item> -->
        </a-space>
      </a-form-item>
      <a-form-item
        label="TestConfig"
        :content-flex="false"
        :merge-props="false"
      >
        <a-form-item
          v-for="(judgeCaseItem, index) of form.judgeCase"
          :key="index"
          no-style
        >
          <a-space direction="vertical" style="min-width: 640px">
            <a-form-item
              :field="`form.judgeCase[${index}].input`"
              :label="`Test-${index}`"
              :key="index"
            >
              <a-input
                v-model="judgeCaseItem.input"
                placeholder="Enter the test"
              />
            </a-form-item>
            <a-form-item
              :field="`form.judgeCase[${index}].output`"
              :label="`Output-${index}`"
              :key="index"
            >
              <a-input
                v-model="judgeCaseItem.output"
                placeholder="Enter the output"
              />
            </a-form-item>
            <a-form-item :label="`Score`" :key="index">
              <a-input placeholder="Enter the score" />
            </a-form-item>
            <a-button status="danger" @click="handleDelete(index)">
              Delete
            </a-button>
          </a-space>
        </a-form-item>
        <div style="margin-top: 32px">
          <a-button @click="handleAdd" type="outline" status="success"
            >Add new test case
          </a-button>
        </div>
      </a-form-item>
      <div style="margin-top: 16px" />
      <a-form-item>
        <a-button type="primary" style="min-width: 200px" @click="doSubmit"
          >submit
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import MdEditor from "@/components/MdEditor.vue";
import { QuestionControllerService } from "../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRoute } from "vue-router";

const route = useRoute();
// 如果页面地址包含 update，视为更新页面
const updatePage = route.path.includes("update");

let form = ref({
  title: "",
  tags: [],
  answer: "",
  content: "",
  judgeConfig: {
    memoryLimit: 10000,
    // stackLimit: 1000,
    timeLimit: 1000,
  },
  judgeCase: [
    {
      input: "",
      output: "",
    },
  ],
});

/**
 * 根据题目 id 获取老的数据
 */
const loadData = async () => {
  const id = route.query.id;
  if (!id) {
    return;
  }
  const res = await QuestionControllerService.getQuestionByIdUsingGet(
    id as any
  );
  if (res.code === 0) {
    form.value = res.data as any;
    // json 转 js 对象
    if (!form.value.judgeCase) {
      form.value.judgeCase = [
        {
          input: "",
          output: "",
        },
      ];
    } else {
      form.value.judgeCase = JSON.parse(form.value.judgeCase as any);
    }
    if (!form.value.judgeConfig) {
      form.value.judgeConfig = {
        memoryLimit: 10000,
        // stackLimit: 1000,
        timeLimit: 1000,
      };
    } else {
      form.value.judgeConfig = JSON.parse(form.value.judgeConfig as any);
    }
    if (!form.value.tags) {
      form.value.tags = [];
    } else {
      form.value.tags = JSON.parse(form.value.tags as any);
    }
  } else {
    message.error("加载失败，" + res.message);
  }
};

onMounted(() => {
  loadData();
});

const changeConfig = (type: number) => {
  if (type === 1) {
    form.value.judgeConfig = {
      memoryLimit: 15000,
      // stackLimit: 1000,
      timeLimit: 2000,
    };
  } else if (type === 2) {
    form.value.judgeConfig = {
      memoryLimit: 20000,
      // stackLimit: 1000,
      timeLimit: 1000,
    };
  } else if (type === 3) {
    form.value.judgeConfig = {
      memoryLimit: 38943,
      // stackLimit: 1000,
      timeLimit: 9485,
    };
  }
};
const doSubmit = async () => {
  console.log(form.value);
  // 区分更新还是创建
  if (updatePage) {
    const res = await QuestionControllerService.updateQuestionUsingPost(
      form.value
    );
    if (res.code === 0) {
      message.success("更新成功");
    } else {
      message.error("更新失败，" + res.message);
    }
  } else {
    const res = await QuestionControllerService.addQuestionUsingPost(
      form.value
    );
    if (res.code === 0) {
      message.success("创建成功");
    } else {
      message.error("创建失败，" + res.message);
    }
  }
};

/**
 * 新增判题用例
 */
const handleAdd = () => {
  form.value.judgeCase.push({
    input: "",
    output: "",
  });
};

/**
 * 删除判题用例
 */
const handleDelete = (index: number) => {
  form.value.judgeCase.splice(index, 1);
};

const onContentChange = (value: string) => {
  form.value.content = value;
};

const onAnswerChange = (value: string) => {
  form.value.answer = value;
};
</script>

<style scoped>
#group {
  width: 300px;
  height: 80px;
  display: flex;
  justify-content: space-between;
}
.addQuestionView {
  z-index: -1;
}
</style>
